import User from './user.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Injectable, Inject } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom, map, Observable } from 'rxjs';
import Utils from 'src/utils';

@Injectable()
export class AppService {
  @Inject()
  private readonly httpService: HttpService;

  @InjectRepository(User)
  private userRepository: Repository<User>;

  @Inject()
  private readonly utils: Utils;


  private parentMessageId = '';


  async proxyChat(prompt: string): Promise<Observable<any>> {
    const chatGPTAPI:any = await this.utils.getChatgpt();
    const reciveMsg = await chatGPTAPI.sendMessage(prompt,  {
      parentMessageId: this.parentMessageId
    })
    console.log(prompt, this.parentMessageId);
    this.parentMessageId = reciveMsg.id;
   
    
    return reciveMsg;
  }

  async findOne(userId: string){
    const res = await this.userRepository.findOne({
      where: [ {id : userId}],
    });
    return res;
  }

  async saveUser(user: User){
    const res = await this.userRepository.save(user);
    return res;
  }
}
